package com.grandbroad.eim.ui.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import com.easy.west.westclient.api.vod.bean.ClassList;
import com.easy.west.westclient.api.vod.event.GetAllClassEvent;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.VodViewPagerAdapter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseActivity;
import com.grandbroad.eim.ui.fragment.AlbumFragment;
import com.grandbroad.eim.ui.fragment.ClassFragment;
import com.grandbroad.eim.ui.fragment.HomeFragment;
import com.grandbroad.eim.ui.fragment.MeFragment;
import com.grandbroad.eim.ui.fragment.UserFragment;
import com.grandbroad.eim.wedgit.MyViewPager;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import net.lucode.hackware.magicindicator.MagicIndicator;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity {


    @BindView(R.id.home_viewpager)
    MyViewPager homeViewpager;
    @BindView(R.id.bottom_bar)
    BottomNavigationViewEx bottomBar;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private HomeFragment homeFragment;
    private AlbumFragment albumFragment;
    private MeFragment userFragment;
    private ClassFragment classFragment;
    private HomeViewpagerAdpter homeViewpagerAdpter;
    @Override
    protected void initview() {
        cofingViewpager();
    }


    @Override
    protected void initdata() {

        //获取分类列表
        MyApplication.getMoleclient().getAllClass();
        initfragmentandtitle();

    }

    private void cofingViewpager() {
        homeViewpagerAdpter = new HomeViewpagerAdpter(getSupportFragmentManager(), fragments, titles);
        homeViewpager.setAdapter(homeViewpagerAdpter);
        homeViewpager.setOffscreenPageLimit(homeViewpagerAdpter.getCount());
        //关闭动画模式
        bottomBar.enableShiftingMode(false);
        bottomBar.enableItemShiftingMode(false);
        bottomBar.enableAnimation(false);
        bottomBar.setupWithViewPager(homeViewpager);
    }

    private void initfragmentandtitle() {
        homeFragment = new HomeFragment();
        classFragment = new ClassFragment();
        albumFragment = new AlbumFragment();
        userFragment = new MeFragment();
        fragments.add(homeFragment);
        fragments.add(classFragment);
        fragments.add(albumFragment);
        fragments.add(userFragment);
        titles.add("首页");
        titles.add("分类");
        titles.add("专辑");
        titles.add("我的");
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_home;
    }

    static class HomeViewpagerAdpter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments;
        private List<String> titles;

        public HomeViewpagerAdpter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }


}
